package com.wzy.mall.web.servlet.client;

import com.alibaba.druid.util.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wzy.mall.model.bo.clientBO.LoginBO;
import com.wzy.mall.model.bo.clientBO.SignupBO;
import com.wzy.mall.model.bo.clientBO.UserPwdBO;
import com.wzy.mall.model.bo.clientBO.UserUpdateDataBO;
import com.wzy.mall.model.vo.ResultVO;
import com.wzy.mall.model.vo.clientVO.LoginVO;
import com.wzy.mall.model.vo.clientVO.UserDataVO;
import com.wzy.mall.service.client.UserService;
import com.wzy.mall.service.client.impl.UserServiceImpl;
import com.wzy.mall.utils.HTTPUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class User {

    private static ObjectMapper jsonMapper = new ObjectMapper();
    private static UserService userService = new UserServiceImpl();

    /**
     *  用户登陆
     * @param request
     * @param response
     */
    public static void login(HttpServletRequest request, HttpServletResponse response) {
        try {
            // 获取JSON数据
            String requestBody = HTTPUtils.getJSONRequest(request);
            LoginBO loginBO = jsonMapper.readValue(requestBody, LoginBO.class);

            // 判空
            if(StringUtils.isEmpty(loginBO.getEmail()) || StringUtils.isEmpty(loginBO.getPwd())) {
                ResultVO resultVO = new ResultVO(10000, null, "用户名和密码不能为空啊 笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
                return;
            }

            // 调用Service层
            LoginVO loginVO = userService.login(loginBO);
            //System.out.println(loginVO);

            if(loginVO == null) {
                ResultVO resultVO = new ResultVO(10000, null, "用户名或密码错误啦！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            } else {

                HttpSession requestSession = request.getSession();
                requestSession.setAttribute("client", loginVO.getToken());

                loginVO.setCode(0);
                ResultVO resultVO = new ResultVO(0, loginVO, null);
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  用户注册
     * @param request
     * @param response
     */
    public static void signup(HttpServletRequest request, HttpServletResponse response) {

        try {
            // 获取JSON数据
            String requestBody = HTTPUtils.getJSONRequest(request);
            // 转换为BO对象
            SignupBO signupBO = jsonMapper.readValue(requestBody, SignupBO.class);

            // 判空
            if(StringUtils.isEmpty(signupBO.getEmail())) {
                ResultVO resultVO = new ResultVO(10000, null, "邮箱不能为空啊！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
                return;
            }
            if(StringUtils.isEmpty(signupBO.getNickname())) {
                ResultVO resultVO = new ResultVO(10000, null, "昵称不能为空啊！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
                return;
            }
            if(StringUtils.isEmpty(signupBO.getPwd())) {
                ResultVO resultVO = new ResultVO(10000, null, "密码不能为空啊！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
                return;
            }
            if(StringUtils.isEmpty(signupBO.getRecipient())) {
                ResultVO resultVO = new ResultVO(10000, null, "收件人不能为空啊！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
                return;
            }
            if(StringUtils.isEmpty(signupBO.getAddress())) {
                ResultVO resultVO = new ResultVO(10000, null, "地址不能为空啊！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
                return;
            }
            if(StringUtils.isEmpty(signupBO.getPhone())) {
                ResultVO resultVO = new ResultVO(10000, null, "电话不能为空啊！");
                response.getWriter().write(jsonMapper.writeValueAsString(resultVO));
                return;
            }

            // 调用Service层
            LoginVO loginVO = userService.signup(signupBO);
            if(loginVO == null) {
                ResultVO error = ResultVO.error("注册失败啊！也许是用户名重复了吧？");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(loginVO);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  根据昵称获取当前用户信息
     * @param request
     * @param response
     */
    public static void data(HttpServletRequest request, HttpServletResponse response) {
        // 键值对参数
        String token = request.getParameter("token");

        // Service
        UserDataVO userDataVO = userService.getUserData(token);
        try {
            if(userDataVO == null) {
                ResultVO error = ResultVO.error("服务器崩溃啦呜呜呜");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(userDataVO);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  用户更改密码
     * @param request
     * @param response
     */
    public static void updatePwd(HttpServletRequest request, HttpServletResponse response) {
        try {
            String requestBody = HTTPUtils.getJSONRequest(request);
            // 封装
            UserPwdBO userPwdBO = jsonMapper.readValue(requestBody, UserPwdBO.class);

            // 判空
            if(StringUtils.isEmpty(userPwdBO.getOldPwd())) {
                ResultVO error = ResultVO.error("给我输入旧密码啊，笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }
            if(StringUtils.isEmpty(userPwdBO.getNewPwd())) {
                ResultVO error = ResultVO.error("给我输入新密码啊，笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }
            if(StringUtils.isEmpty(userPwdBO.getConfirmPwd())) {
                ResultVO error = ResultVO.error("给我再确认一次新密码啊，笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }

            // 确认新密码和确认密码是否一致
            if(!userPwdBO.getNewPwd().equals(userPwdBO.getConfirmPwd())) {
                ResultVO error = ResultVO.error("两次新密码不一致啊，笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }

            // Service
            int result = userService.updatePwdByID(userPwdBO);
            if(result == 0) {
                ResultVO error = ResultVO.error("服务器不太舒服的样子，修改失败啦");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else if(result == 404) {
                ResultVO error = ResultVO.error("旧密码错误啦！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(null);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  更改用户信息
     * @param request
     * @param response
     */
    public static void updateUserData(HttpServletRequest request, HttpServletResponse response) {
        try {
            String requestBody = HTTPUtils.getJSONRequest(request);
            // 封装BO
            UserUpdateDataBO userUpdateDataBO = jsonMapper.readValue(requestBody, UserUpdateDataBO.class);

            // 判空！
            if(StringUtils.isEmpty(userUpdateDataBO.getNickname())) {
                ResultVO error = ResultVO.error("昵称不要留空啊，笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }
            if(StringUtils.isEmpty(userUpdateDataBO.getRecipient())) {
                ResultVO error = ResultVO.error("收件人不要留空啊，笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }
            if(StringUtils.isEmpty(userUpdateDataBO.getAddress())) {
                ResultVO error = ResultVO.error("地址不要留空啊，笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }
            if(StringUtils.isEmpty(userUpdateDataBO.getPhone())) {
                ResultVO error = ResultVO.error("电话不要留空啊，笨蛋！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
                return;
            }

            // Service
            int result = userService.updateUserData(userUpdateDataBO);

            if(result == 0) {
                ResultVO error = ResultVO.error("服务器阵亡啦！信息修改失败啦！");
                response.getWriter().write(jsonMapper.writeValueAsString(error));
            } else {
                ResultVO ok = ResultVO.ok(null);
                response.getWriter().write(jsonMapper.writeValueAsString(ok));
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *  用户登出
     * @param request
     * @param response
     */
    public static void logoutClient(HttpServletRequest request, HttpServletResponse response) {
        try {
            String requestBody = HTTPUtils.getJSONRequest(request);

            // 清除Session的数据
            HttpSession requestSession = request.getSession();
            requestSession.invalidate();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
